package com.epam.edu.task02.Strings;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

import org.apache.log4j.Logger;

public class ConfigurationRegex {
	private static String doubleSpaceAndTab;
	private static String endSentence;
	private static Logger logger = Logger.getLogger(ConfigurationRegex.class);
	private static String punctuation;
	private static String reductions;

	static public String getDoubleSpaceAndTab() {
		return doubleSpaceAndTab;
	}

	static public String getEndSentence() {
		return endSentence;
	}

	public static String getPunctuation() {
		return punctuation;
	}

	static private void makeReductionsRegex() {
		StringBuilder reductionsRegex = new StringBuilder();
		try {
			String filepath = "src/main/resources/reduction.txt";
			FileInputStream fstream = new FileInputStream(filepath);
			DataInputStream reductionsInputStream = new DataInputStream(fstream);
			String inputString;
			BufferedReader bufferedReader = new BufferedReader(
					new InputStreamReader(reductionsInputStream));
			reductionsRegex.append("[^");
			while ((inputString = bufferedReader.readLine()) != null) {
				reductionsRegex.append("(").append(inputString).append(")");
			}
			reductionsRegex.append("]");
		} catch (IOException exception) {
			logger.error(exception.getMessage());
		}
		reductions = reductionsRegex.toString();
	}

	public ConfigurationRegex() {
		Properties props = new Properties();
		try {
			makeReductionsRegex();
			props.load(new FileInputStream(
					"src/main/resources/config.properties"));
			this.doubleSpaceAndTab = props.getProperty("DoubleSpaceAndTab");
			this.endSentence = this.reductions
					+ props.getProperty("EndSentence");
			this.punctuation = this.reductions
					+ props.getProperty("Punctuation");
		} catch (IOException ex) {
			logger.error(ex.getMessage());
		}
	}
}
